package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Reos;
import com.itheima.service.ReposService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 饮食库管理
 */
@RestController
@RequestMapping("/repos")
public class RepositoryController {

@Reference
private ReposService reposService;

    //文件上传，并保存到数据库
       @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        //调用poi的工具类，读取文件
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Reos> reosArrayList = new ArrayList<>();
            //遍历工作表
            //健壮性判断
            if (list !=null  && list.size()>0) {
                //得到每一个行的数据
                for (String[] strings : list) {
                    System.out.println(strings.length);
                    Reos reos = new Reos(strings[0],strings[1],strings[2],strings[3]);
                    //将每一个单元格对象添加到集合中
                    reosArrayList.add(reos);
                    System.out.println(reosArrayList);
                }
                //调用service进行添加到数据库中
                reposService.add(reosArrayList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //上传成功
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    //添加饮食数据
    @RequestMapping("/add")
    public void  addFood(@RequestBody Reos reos){
           reposService.addFood(reos);
    }

    //分页查询
    @RequestMapping("/findpage")
    public PageResult  findpage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=reposService.findpageRepos(queryPageBean);
        return pageResult;
    }

    //回写数据
    @RequestMapping("/findAll")
    public Result coyBack(Integer id){
        try {
            Reos reos=reposService.findAll(id);
            return new Result(true, MessageConstant.QUERY_SUCCESS,reos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_FAIL);
        }
    }

    //编辑饮食库数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Reos reos){
        try {
            reposService.edit(reos);
            return new Result(true, MessageConstant.EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_FAIL);
        }
    }

    //删除数据
    @RequestMapping("/del")
    public Result del(Integer id){
        try {
            reposService.del(id);
            return new Result(true, MessageConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_FAIL);
        }
    }
}
